/**
 * Licensed to the Apache Software Foundation （ASF） under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * （the "License"）； you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * https://www.q3z3.com
 * QQ : 939313737
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package vben.core.framework.sms.service.impl;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import vben.core.common.exception.base.BaseException;
import vben.core.framework.sms.enums.SmsTemplateEnum;
import vben.core.framework.sms.enums.SmsTypeEnum;
import vben.core.framework.sms.service.SmsService;
import vben.core.framework.sms.vo.SmsVo;

import java.util.concurrent.TimeUnit;

/**
 * token 服务层
 */
@Service("smsService")
@Slf4j
public class SmsServiceImpl implements SmsService {

//    @Autowired
//    private RedisUtils redisUtils;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Dict sendSms(SmsVo smsVo) {
        // 验证手机号
        if (!Validator.isMobile(smsVo.getPhone())) {
            throw new BaseException("请输入正确的手机号");
        }
        SmsTypeEnum smsType = smsVo.getType();
        String key = smsType.getPrefix().concat(smsVo.getPhone());
        // 生成验证码
        String code = String.valueOf(RandomUtil.randomInt(1000, 9999));
        // 发送短信
//        if (YesOrNoEnum.YES.equals(PlatformConfig.SMS)) {
        if (true) {
            Dict dict = Dict.create()
                    .set("code", code);
            doSendSms(smsVo.getPhone(), SmsTemplateEnum.VERIFY_CODE, dict);
        }
        // 存入缓存
        redisTemplate.opsForValue().set(key, code, smsType.getTimeout(), TimeUnit.MINUTES);
        return Dict.create().set("code", code).set("expiration", smsType.getTimeout());
    }

    @Override
    public void verifySms(String phone, String code, SmsTypeEnum type) {
        // 验证手机号
        if (!Validator.isMobile(phone)) {
            throw new BaseException("请输入正确的手机号");
        }
        String key = type.getPrefix().concat(phone);
        System.out.println(key);
        if (!redisTemplate.hasKey(key)) {
            throw new BaseException("验证码已过期，请重新获取");
        }
        String value = redisTemplate.opsForValue().get(key);
        if (value.equalsIgnoreCase(code)) {
            redisTemplate.delete(key);
        } else {
            throw new BaseException("验证码不正确，请重新获取");
        }
    }

    /**
     * 执行发送短信
     *
     * @param phone
     * @param templateCode
     * @param dict
     */
    private void doSendSms(String phone, SmsTemplateEnum templateCode, Dict dict) {
        // 短信待集成
    }

}
